/**
 * 
 */
package org.cuatrovientos.erp.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.cuatrovientos.erp.business.EmployeeBOInterface;
import org.cuatrovientos.erp.model.Employee;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.beans.factory.annotation.Autowired;



/**
 * Controller class form Employee management
 * @author pablo_guerrero
 */
@Controller
public class EmployeeController {

	// Get log4j logger
	private static final Logger logger = Logger.getLogger(EmployeeController.class);

	@Autowired
    private EmployeeBOInterface employeeBO;
    
        /**
         * default constructor
         */
        public EmployeeController () {
               logger.info("Employee BEAN instantiated");
                
        }
        
        
        
        /**
         * handles default /employee or /employee/list request
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/employee","/employee/list"})
         */
        @RequestMapping({"/employee","/employee/list"})
        public ModelAndView employeeDefault() {
        	logger.info("default");
 
                Map<String, Object> model = new HashMap<String, Object>();

                List<Employee> employeeList = employeeBO.selectAll();
                model.put("employees", employeeList );
                return new ModelAndView("EmployeeList", model);     
        }

        /**
         * handles /employee/search request, redirected to default page
         * @param model
         * @return the name of the view to show
         * @RequestMapping({"/employee/search"})
         */
        @RequestMapping(method=RequestMethod.POST,value={"/employee/search"})
        public ModelAndView search(@RequestParam(value="description", defaultValue="", required=true) String description) {
        	logger.info("search");
 
            Map<String, Object> model = new HashMap<String, Object>();

            List<Employee> employeeList = employeeBO.search(description);
            model.put("employees", employeeList );
            
            return new ModelAndView("EmployeeList", model);     
        }

        /**
         * handles delete employee, and redirect to employee default
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/employee","/employee/list"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/employee/delete"})
        public String deleteEmployee(@RequestParam(value="id", defaultValue="", required=true) int id) {
        	logger.info("delete " + id);

        	employeeBO.deleteById(id);
            return "redirect:/employee";

        }
        
        /**
         * handles new employee request. In the jsp we are using spring's form:form tags
         * Spring expects a model called by default 'command' or any name
         * we set to it: in this case "role"
         * @return the name of the view to show
         * @RequestMapping({"/employee/new"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/employee/new"})
        public ModelAndView newEmployee() {
        		logger.info("new employee ");
        		Employee employee = new Employee();
               
                return new ModelAndView("EmployeeNew", "employee",employee);
        }
        
        /**
         * handles new employee request. In the jsp we are using spring's form:form tags
         * Spring expects a model called by default 'command' or any name
         * we set to it.
         * @return the name of the view to show
         * @RequestMapping({"/employee/new"})
         */
        @RequestMapping(method=RequestMethod.POST,value={"/employee/save"})
        public String newEmployee(@Valid Employee employee, BindingResult bindingResult) {
        		logger.info("new employee " + employee.toString());
        		
        		// In case of validation errors, go back to form
        		if (bindingResult.hasErrors()) {
        			Map<String, Object> model = new HashMap<String, Object>();
        			model.put("employee", employee);
        			return "EmployeeNew";
        		}
        		
        		// If data is ok, insert and go on
                employeeBO.insert(employee);
                return "redirect:/employee";
        }
        
        /**
         * handles delete employee, and redirect to employee default
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/employee","/employee/list"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/employee/detail"})
        public ModelAndView employeeDetail(@RequestParam(value="id", defaultValue="", required=true) int id) {
        	logger.info("Detail " + id);
            Map<String,Object> model = new HashMap<String,Object>();
            Employee employee = employeeBO.selectById(id);
            model.put("employee", employee);
            
            return new ModelAndView("EmployeeDetail",model);

        }
        
        /**
         * handles employee update, first part: register loaded
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/employee/update"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/employee/update"})
        public ModelAndView employeeUpdate(@RequestParam(value="id", defaultValue="", required=true) int id) {
        	logger.info("update " + id);
                Map<String,Object> model = new HashMap<String,Object>();
                Employee employee = employeeBO.selectById(id);
                model.put("employee", employee);
               return new ModelAndView("EmployeeUpdate",model);

        }
        
        /**
         * handles save update employee request. In the jsp we are using spring's form:form tags
         * Spring expects a model called by default 'command' or any name
         * we set to it.
         * @return the name of the view to show
         * @RequestMapping({"/employee/saveUpdate"})
         */
        @RequestMapping(method=RequestMethod.POST,value={"/employee/saveupdate"})
        public String saveUpdate(@Valid Employee employee, BindingResult bindingResult) {
        		logger.info("update employee " + employee.toString());
        		
        		// In case of validation errors, go back to form
        		if (bindingResult.hasErrors()) {
        			Map<String, Object> model = new HashMap<String, Object>();
        			model.put("employee", employee);
        			return "EmployeeUpdate";
        		}
        		
        		employeeBO.update(employee);
                return "redirect:/employee";
        }
}
